package com.zzx.service.impl;

import com.zzx.utils.EntityUtils;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.entity.TProductGxSubmit;
import com.zzx.mapper.TProductGxSubmitMapper;
import com.zzx.service.TProductGxSubmitService;
import com.zzx.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author zzx
 * @description 针对表【t_product_gx_submit(产品工序提交记录)】的数据库操作Service实现
 * @createDate 2025-07-24
 */
@Service
public class TProductGxSubmitServiceImpl extends ServiceImpl<TProductGxSubmitMapper, TProductGxSubmit> implements TProductGxSubmitService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TProductGxSubmit param) {

        if (ObjectUtils.isEmpty(param.getProductGxSubmitId())) {
            param.setProductGxSubmitId(IdUtil.getSnowflakeNextId());
        } else {
            TProductGxSubmit dbEntity = this.getBaseMapper().selectById(param.getProductGxSubmitId());
            if (dbEntity != null) {
                EntityUtils.mergeNonNullProperties(dbEntity, param);
            }
        }
        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TProductGxSubmit param) {
        this.removeById(param.getProductGxSubmitId());
    }

    @Override
    public IPage<TProductGxSubmit> selectBy(TProductGxSubmit param) {
        LambdaQueryWrapper<TProductGxSubmit> wrapper = new LambdaQueryWrapper<>(TProductGxSubmit.class);

        if (!ObjectUtils.isEmpty(param.getProductGxSubmitId())) {
            wrapper.eq(TProductGxSubmit::getProductGxSubmitId, param.getProductGxSubmitId());
        }

        // 多租户拦截器会自动添加 company_id 条件
        wrapper.orderByDesc(TProductGxSubmit::getCreateTime);

        return this.getBaseMapper().selectPage(PageUtils.createPage(param.getPageNum(), param.getPageSize()), wrapper);
    }

    @Override
    public TProductGxSubmit findById(String id) {
        return this.getBaseMapper().selectById(id);
    }
}
